package com.hnyyjsxy.dao.impl;

import com.hnyyjsxy.dao.SuperAdmainDao;
import com.hnyyjsxy.domain.Teacher;
import com.hnyyjsxy.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * @date 2021/02/07
 */
public class SuperAdmainDaoImpl implements SuperAdmainDao {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());
    /**
     * 将宿管信息添加至宿管信息表中
     * @param teacher 宿管信息
     */
    @Override
    public void addTeacher(Teacher teacher) {
        String sql = "INSERT INTO teacher(`account`, `tname`, `gender`, `phone`, `building`) VALUES (?,?,?,?,?)";
        jdbcTemplate.update(sql,teacher.getAccount(),teacher.getTname(),teacher.getGender(),teacher.getPhone(),teacher.getBuilding());
    }

    /**
     * 将宿管添加至账号表中
     * @param account 账号
     * @param password 密码
     */
    @Override
    public void addTeacherAccount(String account, String password) {
        String sql = "INSERT INTO account_sheet(account,a_password,identity) VALUE(?,?,2)";
        jdbcTemplate.update(sql,account,password);
    }

    /**
     * 查询所有宿管老师信息
     * @return
     */
    @Override
    public List<Teacher> queryAllTeacher() {
        List<Teacher> list = null;
        try{
            String sql = "SELECT * FROM `teacher`";
            list = jdbcTemplate.query(sql,new BeanPropertyRowMapper<Teacher>(Teacher.class));
        }catch(Exception e){

        }
        return list;
    }

    /**
     * 根据账号删除某个宿管老师信息
     * @param account 宿管老师账号
     */
    @Override
    public void delTeacherByAccount(String account) {
        //宿管老师信息表
        String sql = "DELETE FROM teacher WHERE account=?";
        jdbcTemplate.update(sql,account);
        //账号表
        String sql1 = "DELETE FROM account_sheet WHERE account=?";
        jdbcTemplate.update(sql1,account);
    }
}
